package com.qiantu.phone.action;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;

import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.widget.StatusLayout;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/12/08
 *    desc   : 状态布局意图
 */
public interface StatusAction {

    /**
     * 获取状态布局
     */
    StatusLayout getStatusLayout();

//    /**
//     * 显示加载中
//     */
//    default void showLoading() {
//        showLoading(R.raw.loading);
//    }

//    default void showLoading(@RawRes int id) {
//        StatusLayout layout = getStatusLayout();
//        layout.show();
//        layout.setAnimResource(id);
//    }

    default void showLoading() {
        showLoading(R.drawable.img_loading);
    }

    default void showLoading(@DrawableRes int id) {
        StatusLayout layout = getStatusLayout();
        layout.show();
        layout.setAnimResource(id);
    }

    /**
     * 显示加载完成
     */
    default void showComplete() {
        StatusLayout layout = getStatusLayout();
        if (layout == null || !layout.isShow()) {
            return;
        }
        layout.hide();
    }

    /**
     * 显示空提示
     */
    default void showEmpty(int icon, @StringRes int hint, @StringRes int btnText, StatusLayout.OnBtnClickListener listener) {
        showLayout(icon, hint, btnText, listener);
    }

    /**
     * 显示错误提示
     */
    default void showError(int icon, StatusLayout.OnBtnClickListener listener) {
        StatusLayout layout = getStatusLayout();
        Context context = layout.getContext();
        ConnectivityManager manager = ContextCompat.getSystemService(context, ConnectivityManager.class);
        if (manager != null) {
            NetworkInfo info = manager.getActiveNetworkInfo();
            // 判断网络是否连接
            if (info == null || !info.isConnected()) {
                showLayout(icon, R.string.status_layout_error_network, R.string.retry, listener);
                return;
            }
        }
        showLayout(icon, R.string.status_layout_error_request, R.string.retry, listener);
    }

    /**
     * 显示错误提示
     */
    default void showError(int icon, @StringRes int hint,  StatusLayout.OnBtnClickListener listener) {
        StatusLayout layout = getStatusLayout();
        Context context = layout.getContext();
        ConnectivityManager manager = ContextCompat.getSystemService(context, ConnectivityManager.class);
        if (manager != null) {
            NetworkInfo info = manager.getActiveNetworkInfo();
            // 判断网络是否连接
            if (info == null || !info.isConnected()) {
                showLayout(icon, hint, R.string.retry, listener);
                return;
            }
        }
        showLayout(icon, hint, R.string.retry, listener);
    }

    /**
     * 显示自定义提示
     */
    default void showLayout(int drawableId, @StringRes int hint,  @StringRes int btnText, StatusLayout.OnBtnClickListener listener) {
        StatusLayout layout = getStatusLayout();
        Context context = layout.getContext();
        showLayout(AppApplication.getInstance().getThemeDrawable(drawableId), context.getString(hint), context.getString(btnText), listener);
    }

    default void showLayout(Drawable drawable, CharSequence hint, CharSequence btnText, StatusLayout.OnBtnClickListener listener) {
        StatusLayout layout = getStatusLayout();
        layout.show();
        layout.setIconAndHint(drawable, hint);
        layout.setOnBtnListener(btnText, listener);
    }
}